package com.xiangying.service.impl;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

import org.springframework.transaction.annotation.Transactional;

import com.xiangying.dao.BaseDao;
import com.xiangying.entity.common.Pageable;
import com.xiangying.entity.common.Pagination;
import com.xiangying.service.BaseService;
import com.xiangying.utils.MyUtils;
import com.xiangying.utils.ReflectHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * Service - 基类
 * 
 * @author luolihong
 * @version 1.0
 */
public class BaseServiceImpl<T, D extends Serializable> implements BaseService<T, D>{
	
	/** "ID"属性名称 */
	private static String ID_PROPERTY_NAME = "id";
	
	private BaseDao<T, D> baseDao;
  
	public void setBaseDao(BaseDao<T, D> baseDao) {
		this.baseDao = baseDao;
	}

	@Override
	public T selectByPrimaryKey(D id) {
		return baseDao.selectByPrimaryKey(id);
	}
	
	public T selectByParam(Map<String, Object> param) {
		return baseDao.selectByParam(param);
	}

	public List<T> findList(Map<String, Object> param){
		return baseDao.findList(param);
	}

	/**
	 * 分页查询
	 */
	public Pagination<T> findPage(Pageable pageable){
		// 调用分页插件
		PageHelper.startPage(pageable.getPageNumber(), pageable.getPageSize());
		PageInfo<T> pageInfo = new PageInfo<T>(findList(pageable.getParam()));
		
		// 封装成自己需要的对象
		Pagination<T> pagination = new Pagination<T>();
		pagination.setPageSize(pageInfo.getPageSize());
		pagination.setPageNumber(pageInfo.getPageNum());
		pagination.setTotal(pageInfo.getTotal());
		pagination.setRows(pageInfo.getList());
		pagination.setTotalPage(pageInfo.getPages());
		return pagination;
	}

	@Transactional
	public int insert(T entity) {
		try {
			Field field = entity.getClass().getDeclaredField(ID_PROPERTY_NAME);
			if(field != null) {
				Object value = null;
				if (field.isAccessible()) {
					value = field.get(entity);
				} else {
					field.setAccessible(true);
					value = field.get(entity);
					field.setAccessible(false);
				}
				if(value == null) {
					ReflectHelper.setValueByFieldName(entity, ID_PROPERTY_NAME, MyUtils.GenerateTimeID());
				}
			}
			return baseDao.insert(entity);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return baseDao.insert(entity);
	}
	
	@Transactional
	public T insertReturn(T entity) {
		if (insert(entity) > 0)
			return entity;
		return null;
	}
	
	@Transactional
	public int insertSelective(T entity){
		try {
			Field field = entity.getClass().getDeclaredField(ID_PROPERTY_NAME);
			if(field != null) {
				Object value = null;
				if (field.isAccessible()) {
					value = field.get(entity);
				} else {
					field.setAccessible(true);
					value = field.get(entity);
					field.setAccessible(false);
				}
				if(value == null) {
					ReflectHelper.setValueByFieldName(entity, ID_PROPERTY_NAME, MyUtils.GenerateTimeID());
				}
			}
			return baseDao.insertSelective(entity);
		} catch (Exception e) {
			e.printStackTrace();
		}
	    return baseDao.insertSelective(entity);
	}
	
	/**
	 * 批量插入
	 * @param entitys
	 * @return
	 */
	@Transactional
	public int insertBatch(List<T> entitys){
		return baseDao.insertBatch(entitys);
	}

	@Transactional
	public int updateByPrimaryKey(T entity) {
		return baseDao.updateByPrimaryKey(entity);
	}
	
	@Transactional
	public int updateByPrimaryKeySelective(T entity) {
		return baseDao.updateByPrimaryKeySelective(entity);
	}
	
	@Transactional
	public void updateForBatch(List<T> entityList) {
		baseDao.updateForBatch(entityList);
	}

	@Transactional
	public void deleteByPrimaryKey(D id) {
		baseDao.deleteByPrimaryKey(id);
	}
	
	@Transactional
	public void deleteForBatch(D[] ids) {
		baseDao.deleteForBatch(ids);
	}
}